//
//  F_1ViewController.m
//  RACExamale
//
//  Created by cym_bj on 2017/12/4.
//  Copyright © 2017年 com.cym. All rights reserved.
//

#import "F_1ViewController.h"
#import <ReactiveObjc/ReactiveObjC.h>
#import "MBProgressHUD+XMG.h"
@interface F_1ViewController ()

@property (weak ,nonatomic) IBOutlet UITextField *accountTF;

@property (weak ,nonatomic) IBOutlet UITextField *pwdTF;
@property (weak, nonatomic) IBOutlet UIButton *logBtn;

@end

@implementation F_1ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"登录";
    
    
    // 1 、处理文本框业务逻辑
    
    RACSignal *loginEnablesSignal=[RACSignal combineLatest:@[self.accountTF.rac_textSignal,self.pwdTF.rac_textSignal] reduce:^id _Nullable(NSString*account,NSString *pwd){
        
        return @(account.length&&pwd.length);
    }];
    
    [loginEnablesSignal subscribeNext:^(id  _Nullable x) {

        //设置按钮状态
        BOOL enable=[x boolValue];
        self.logBtn.userInteractionEnabled=enable;
        self.logBtn.alpha=enable?1.0f:0.4f;
        
            
    }];
    

    
    // 3、 创建登录命令
    
    RACCommand *command=[[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        
        NSLog(@"哈哈:%@",input);
        
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
           
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                NSLog(@"ddd");
                
                [subscriber sendNext:@"请求登录的数据"];
                //发送完毕
                [subscriber sendCompleted];
                
            });
            
           
            return nil;
        }];
        
    }];
    
    
    //获取命令中信号源
    [command.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
       
        NSLog(@"%@",x);
    }];
    
    
   
    //监听命令执行过程
    [command.executing subscribeNext:^(NSNumber * _Nullable x) {
        
        if ([x boolValue]==YES) {
            
            [MBProgressHUD showMessage:@"正在登录ing.."];
            
        }
        else
        {
            //执行完成
           [MBProgressHUD hideHUD];
            
        }
        
        
    }];
    
    
    
    //监听登录按钮点击
    [[self.logBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        
        //处理登录事件
        [command execute:@"标识"];
        
    }];
    
   
    
    
    
}



@end
